package tms.util.jabber;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;

public class JabberPacketListener implements PacketListener {
    private XMPPConnection connection;


    public JabberPacketListener(XMPPConnection connection) {
        this.connection = connection;
    }

    
    @Override
    public void processPacket(Packet packet) {
        Presence presence = (Presence)packet;
        String userId = connection.getRoster().getPresence(presence.getFrom()).getFrom();
        String[] tokens = userId.split("/");
        if (tokens.length > 0) {
            userId = tokens[0];
            JabberManager.updateUserStatus(userId, JabberManager.JabberUser.Status.parse(presence.getType(), presence.getMode()));
        }
    }
}
